package com.java.controller;


import com.java.pojo.EnterGoods;
import com.java.pojo.Page;
import com.java.service.EnterGoodsService;
import com.java.service.IntoAndOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 出入库
 */
@Controller
@RequestMapping("/IntoAndOut")
public class IntoAndOut {

    @Autowired
    private EnterGoodsService enterGoodsService;
    @Autowired
    private IntoAndOutService intoAndOutService;

    @RequestMapping(value = "/login", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String s() {
        return "<script>alert('欢迎回来');location.href='/IntoAndOut/EnterFindAll';</script>";
    }


    /*出库前*/
    @RequestMapping("/EnterFindAll")
    public String findAll(@RequestParam(value = "start", required = false) Integer start,
                          @RequestParam(value = "documentid", required = false) String documentid,
                          Model model) {
        if (documentid == null || "".equals(documentid)) {
            documentid = "";
        }
        if (start == null || start.equals("")) {
            start = 1;
        }
        System.out.println(start + "+" + documentid);
        /*总个数*/
        Integer count = intoAndOutService.getCount(documentid,0);
        int i = count % 4 == 0 ? count / 4 : (count / 4) + 1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if (start >= i) {
            start = i;
        } else if (start <= 1) {
            start = 1;
        }
        Page<com.java.pojo.IntoAndOut> page = intoAndOutService.getPageByPage(start, documentid);
        System.out.println("page:" + page);
        model.addAttribute("page", page);
        model.addAttribute("document", documentid);
        return "IntoAndOut/findAllByPage";
    }


    /*出库后*/
    @RequestMapping("/OutFindAll")
    public String OutFindAll(@RequestParam(value = "start", required = false) Integer start,
                             @RequestParam(value = "documentid", required = false) String documentid,
                             Model model) {
        if (documentid == null || "".equals(documentid)) {
            documentid = "";
        }
        if (start == null || start.equals("")) {
            start = 1;
        }
        System.out.println(start + "+" + documentid);
        /*总个数*/
        Integer count = intoAndOutService.getCount(documentid,1);
        int i = count % 4 == 0 ? count / 4 : (count / 4) + 1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if (start >= i) {
            start = i;
        } else if (start <= 1) {
            start = 1;
        }
        Page<com.java.pojo.IntoAndOut> page = intoAndOutService.getPageByPage2(start, documentid);
        System.out.println("page:" + page);
        model.addAttribute("page", page);
        model.addAttribute("document", documentid);
        return "IntoAndOut/findAllByPage2";
    }

    //错误页面
    @RequestMapping("/a")
    public String s2(Model model){
        Page<com.java.pojo.IntoAndOut> page = intoAndOutService.getPageByPage2(1, "");
        model.addAttribute("pa",page);
        return "err";
    }



    /*查询*/
    @RequestMapping("/findAllInOut")
    public String findAllInOut(@RequestParam("eid") Integer eid,
                               @RequestParam("document") String document,
                               Model model
    ) {
        List<EnterGoods> list = enterGoodsService.getListByGoodS(eid, document);
        System.out.println(document + "===========");
        model.addAttribute("list", list);
        model.addAttribute("eid", eid);
        model.addAttribute("document", document);
        return "IntoAndOut/showEnterGoods2";
    }


    //出库操作
    @RequestMapping(value = "out", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String out(@RequestParam(value = "egid", required = false) Integer egid) {
        int i = intoAndOutService.update2(egid);
        if (i > 0) {
            return "<script>alert('出库successful');location.href='/IntoAndOut/OutFindAll';</script>";
    } else {
            return "<script>alert('出库失败);location.href='/EnterGood/EnterPageGood';</script>";
        }
    }
}
